﻿using ArchivingDataModel;
using ArchivingDataWebForms.Helpers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace ArchivingDataWebForms.Pages.Employees
{
    public partial class ViewEmployees : WebFormBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.getOrgs();
            }
        }

        protected void AdvancedRadGridResults_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            using (ArchivingDataEntities ent = new ArchivingDataEntities())
            {
                long orgId = Convert.ToInt64(this.RadComboBoxOrgs.SelectedValue);
                Employee[] items = ent.Employees.Where(i=>i.Organization.Organization_ID==orgId).ToArray();
                this.AdvancedRadGridResults.DataSource = items;
            }
        }
        protected void AdvancedRadGridResults_ItemCommand(object sender, GridCommandEventArgs e)
        {
        }
        protected void AdvancedRadGridResults_InsertCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                using (ArchivingDataEntities ent = new ArchivingDataEntities())
                {
                    GridEditableItem editedItem = e.Item as GridEditableItem;
                    Hashtable newValues = new Hashtable();
                    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
                    string code = newValues["Employee_Number"].ToString();
                    string nameAr = newValues["Employee_NameAR"].ToString();
                    string email = newValues["Email"].ToString();
                    string uniquetext = WebHelper.Helpers.StringHelper.ToUniqueStatement(code + nameAr + email);
                    long orgID = Convert.ToInt64(this.RadComboBoxOrgs.SelectedValue);
                    
                    ent.AddToEmployees(new Employee
                    {
                        Organization=ent.Organizations.FirstOrDefault(i=>i.Organization_ID==orgID),
                        Employee_Number = code,
                        Employee_NameAR = nameAr,
                        Employee_NameEN = nameAr,
                        Email = email,
                        Mobile="",
                        InnerPhone="",
                        Employees_Search = uniquetext
                    });
                    ent.SaveChanges();
                    this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
                }
            }
            catch (Exception er)
            {
                this.ShowNotification("حدث الخطا التالي اثناء الحفظ " + er.Message, NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void AdvancedRadGridResults_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                using (ArchivingDataEntities ent = new ArchivingDataEntities())
                {
                    GridEditableItem editedItem = e.Item as GridEditableItem;
                    Hashtable newValues = new Hashtable();
                    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
                    long id = Convert.ToInt64(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Employee_ID"]);
                    string code = newValues["Employee_Number"].ToString();
                    string nameAr = newValues["Employee_NameAR"].ToString();
                    string email = newValues["Email"].ToString();
                    string uniquetext = WebHelper.Helpers.StringHelper.ToUniqueStatement(code + nameAr + email);
                    var item = ent.Employees.FirstOrDefault(i => i.Employee_ID == id);
                    long orgID = Convert.ToInt64(this.RadComboBoxOrgs.SelectedValue);
                    item.Organization = ent.Organizations.FirstOrDefault(i => i.Organization_ID == orgID);
                    item.Employee_Number = code;
                    item.Employee_NameAR = nameAr;
                    item.Email = email;
                    item.Mobile=item.InnerPhone="";
                    item.Employees_Search = uniquetext;
                    ent.SaveChanges();
                    this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
                }
            }
            catch (Exception er)
            {
                this.ShowNotification("حدث الخطا التالي اثناء الحفظ " + er.Message, NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void AdvancedRadGridResults_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                using (ArchivingDataEntities ent = new ArchivingDataEntities())
                {
                    long id = Convert.ToInt64(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Employee_ID"]);
                    ent.DeleteObject(ent.Employees.FirstOrDefault(i => i.Employee_ID == id));
                    ent.SaveChanges();
                    this.ShowNotification("تم الحذف بنجاح", NotificationIconType.Ok);
                }
            }
            catch (Exception er)
            {
                this.ShowNotification("حدث الخطا التالي اثناء الحذف " + er.Message, NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void AdvancedRadGridResults_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            //{
            //    //width
            //    GridEditableItem editedItem = e.Item as GridEditableItem;
            //    GridEditManager editMan = editedItem.EditManager;
            //    GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)(editMan.GetColumnEditor("PropertyNote_Ar"));
            //    editor.TextBoxMode = TextBoxMode.MultiLine;
            //    editor.TextBoxControl.Width = Unit.Pixel(300);
            //    editor.TextBoxControl.Height = Unit.Pixel(40);
            //    GridTextBoxColumnEditor editor2 = (GridTextBoxColumnEditor)(editMan.GetColumnEditor("PropertyNote_En"));
            //    editor2.TextBoxMode = TextBoxMode.MultiLine;
            //    editor2.TextBoxControl.Width = Unit.Pixel(300);
            //    editor2.TextBoxControl.Height = Unit.Pixel(40);
            //}
        }
        protected void RadComboBoxOrgs_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            e.Item.Text = e.Item.DataItem.GetType().GetProperty("Type_Of_Organization_NameAR").GetValue(e.Item.DataItem, null).ToString()
                + ": " + e.Item.DataItem.GetType().GetProperty("Organization_Number").GetValue(e.Item.DataItem, null).ToString()
                +"- "+ e.Item.DataItem.GetType().GetProperty("Organization_Name_AR").GetValue(e.Item.DataItem, null).ToString();
        }

        private void getOrgs()
        {
            try
            {
                using (ArchivingDataEntities ent = new ArchivingDataEntities())
                {
                    var orgs = ent.Organizations.Include("TypeOfOrganization").Where(i => i.Is_External == 0).Select(i => new { i.Organization_ID, Type_Of_Organization_NameAR = i.TypeOfOrganization.Type_Of_Organization_NameAR, i.Organization_Name_AR, i.Organization_Number }).ToArray();
                    this.RadComboBoxOrgs.DataSource=orgs;
                    this.RadComboBoxOrgs.DataValueField = "Organization_ID";
                    this.RadComboBoxOrgs.DataBind();
                }
            }
            catch (Exception er)
            {
                this.ShowNotification("حدث الخطا التالي اثناء الحفظ " + er.Message, NotificationIconType.Error);
                return;
            }
        }


    }
}